let express = require('express');
let TipOffTables = require('../db/TopOffData/TipOffTables');
const UserCountTables = require("../db/UserCountDB/UserTable");
let BlogTables = require('../db/BlogDataDB/BlogTable');
let tipOffApp = express();
tipOffApp.post('/article', function (req,res){
    TipOffTables.create({
        blogId: req.body.blogId,
        description: req.body.description,
        cover: req.body.cover,
        title: req.body.title,
        reason: req.body.reason
    }).then(rs => {
        res.send({
            status:200,
            message: '举报文章成功'
        })
    })
});

//获得举报的信息列表
tipOffApp.get("/tipOffList",async function (req,res){

    UserCountTables.find({
        token: req.headers.authorization
    }).then(async (rs)=>{
        if (rs.length && rs[0].isAdmin){
            //获取分页
            let {limit,offset}=req.query
            let totalNum=0
            await TipOffTables.find({},{
            }).then(rs=>{
                totalNum=rs.length
            })

            //查询
            TipOffTables.find({},{
                // _id:false,
                __v:false
            },{
                skip: Number(offset),
                limit: Number(limit)
            }).then(rs=>{
                res.send({
                    status:200,
                    message:"查询成功",
                    data:{
                        tipOffTotalNum:totalNum,
                        tipOffList:rs
                    }
                })
            })
        }else{
            res.send({
                status: 401,
                message: '没有操作权限'
            })
        }
    })

})

//或略举报 删除该举报
tipOffApp.delete("/deleteTipOff/:tipOffId",function (req,res){
    UserCountTables.find({
        token: req.headers.authorization
    }).then(async (rs)=>{
        if (rs.length && rs[0].isAdmin){
            //根据id 删除
            let tipOffId=req.params.tipOffId

            TipOffTables.deleteOne({
                _id:tipOffId
            }).then(rs=>{
                res.send({
                    status:200,
                    message:"举报已删除",
                })
            })
        }else{
            res.send({
                status: 401,
                message: '没有操作权限'
            })
        }
    })
})
tipOffApp.delete('/article/:tipOffId',async function (req,res){
    await TipOffTables.find({
        _id: req.params.tipOffId
    }).then((rs) => {
        BlogTables.deleteOne({
            blogId: rs[0].blogId
        }).then(()=>{
            console.log('删除成功')
        })
    })

    TipOffTables.deleteOne({
        _id: req.params.tipOffId
    }).then(rs=>{
        res.send({
            status: 200,
            message: '删除成功'
        })
    })
});
module.exports ={
    tipOffApp
}